package com.jy.novel.dao.system;

import com.jy.novel.common.JdbcHelper;
import com.jy.novel.common.Page;
import com.jy.novel.pojo.form.UserEditForm;
import com.jy.novel.pojo.form.UserQueryForm;
import com.jy.novel.pojo.system.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by jihb on 4/14/16.
 */
@Repository
public class UserDao {

    @Autowired
    private JdbcHelper dbHelper;

    public Page<UserInfo> findUserPage(UserQueryForm form, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select a.*,b.name as roleName from system_user a left join system_role b on a.roleId=b.id and 1=1 ");
        ArrayList params = new ArrayList();
        if (form!=null){
            if(StringUtils.isNotBlank(form.getLoginName())) {
                sql.append(" and (a.loginName like ? or a.nickName like ? ) ");
                params.add("%" + form.getLoginName() + "%");
                params.add("%" + form.getLoginName() + "%");
            }

            if(form.getRoleId()!=null&&form.getRoleId() > 0) {
                sql.append(" and a.roleId = ? ");
                params.add(form.getRoleId());
            }

            if(form.getIsLock()!=null&&form.getIsLock() > 0) {
                sql.append(" and a.isLock = ?");
                params.add(form.getIsLock());
            }
        }
        sql.append(" order by createTime desc");
        return this.dbHelper.queryForPageBean(sql.toString(), params.toArray(), UserInfo.class, curPage, pageSize);
    }

    public UserInfo getUserInfo(String loginName) {
        String sql = "select a.*,b.name as roleName from system_user a left join system_role b on a.roleId=b.id where a.loginName = ? ";
        return (UserInfo)this.dbHelper.queryForBean(sql,new Object[]{loginName},UserInfo.class);
    }

    public boolean existUserInfo(String loginName) {
        String sql = "select count(*) from system_user where loginName = ? ";
        return this.dbHelper.queryForNumber(sql, new Object[]{loginName}).longValue() > 0L;
    }

    public int addUserInfo(UserInfo userInfo) {
        HashMap cl = new HashMap();
        cl.put("loginName", userInfo.getLoginName());
        cl.put("loginPass", userInfo.getLoginPass());
        cl.put("nickName", userInfo.getNickName());
        cl.put("isLock", Integer.valueOf(0));
        cl.put("isDel",0);
        cl.put("roleId", userInfo.getRoleId());
        cl.put("createTime", new Date());
        cl.put("nickName", userInfo.getNickName());
        return this.dbHelper.simpleInsert("system_user", cl);
    }

    public int changeUserIsLock(String loginName, Integer isLock) {
        String sql = "update system_user set isLock = ? where loginName = ? ";
        return this.dbHelper.update(sql, new Object[]{isLock, loginName});
    }

    public int batChangeUserRole(Long sRoleId, Long dRoleId) {
        String sql = "update system_user set roleId = ? where roleId = ? ";
        return this.dbHelper.update(sql, new Object[]{dRoleId, sRoleId});
    }

    public int modifyUserPwd(String loginName, String pwd) {
        String sql = "update system_user set loginPass = ? where loginName = ? ";
        return this.dbHelper.update(sql, new Object[]{pwd, loginName});
    }

    public int updateLoginTime(String loginName) {
        String sql = "update system_user set loginTime = ? where loginName = ? ";
        return this.dbHelper.update(sql, new Object[]{new Date(), loginName});
    }

    public int updateUserInfo(UserEditForm form) {
        StringBuilder sql = new StringBuilder("update system_user set nickName = ?");
        ArrayList params = new ArrayList();
        params.add(form.getNickName());
        if (form.getRoleId()!=null){
            sql.append(",roleId = ? ");
            params.add(form.getRoleId());
        }
        sql.append(" where loginName = ? ");
        params.add(form.getLoginName());
        return this.dbHelper.update(sql.toString(),params.toArray());
    }

    public int deleteUser(String loginName) {
        String sql = "delete from system_user where loginName = ? ";
        return this.dbHelper.update(sql, new Object[]{loginName});
    }
    
}
